package rpg.animation
{
	import flash.utils.Dictionary;

	import rpg.Utils.StringUtil;

	/**
	 * 每个人物动画的信息,和每帧的信息
	 * @author hyy
	 *
	 */
	public class AvatarDescription
	{
		public var id : String;
		public var name : String;
		/**
		 * 人物中心X
		 */
		public var centerX : int;
		/**
		 * 人物中心点Y
		 */
		public var centerY : int;
		/**
		 * 人物的宽度
		 */
		public var width : int;
		/**
		 * 人物的高度
		 */
		public var height : int;
		/**
		 * 加载的版本号
		 */
		public var version : int;
		/**
		 * 一个人物有几个部位的动画名称
		 */
		public var animationPartTypes : Array = [];
		/**
		 * 每个人物的各个部件的动画描述
		 */
		public var actionsDescription : Dictionary = new Dictionary();


		public function AvatarDescription(xml : XML = null)
		{
			parse(xml);
		}

		/**
		 * 解析xml
		 */
		public function parse(xml : XML) : void
		{
			this.name = xml.@name;
			this.width = xml.@width;
			this.height = xml.@height;
			this.version = xml.@version;
			this.centerX = xml.@centerX;
			this.centerY = xml.@centerY;
			this.animationPartTypes = xml.@order.split(",");

			for each(var actionXml : XML in xml.action)
			{
				var actionDes : AvatarActionDescription = new AvatarActionDescription();
				actionDes.actionName = actionXml.@name;
				actionDes.frameCount = actionXml.@frameCount;
				actionDes.hasCenterInfo = actionXml.@hasCenterInfo;
				actionDes.centerX = actionXml.@centerX;
				actionDes.centerY = actionXml.@centerY;
				actionDes.width = actionXml.@width;
				actionDes.height = actionXml.@height;
				actionDes.attackFrame = actionXml.@attackFrame;
				actionDes.soundFrame = actionXml.@soundFrame;
				actionDes.dirs = actionXml.@dirs.split(",");

				for each(var partXML : XML in actionXml.partType)
				{
					var partType : String = String(partXML.@name);
					for each(var partItemXML : XML in partXML.partItem)
					{
						var partDesc : AvatarPartDescription = new AvatarPartDescription();
						partDesc.partType = partType;
						partDesc.partName = partItemXML.@name;
						for each(var dirStr : String in actionDes.dirs)
						{
							partDesc.partResId[dirStr] = StringUtil.concatStr("/" , name , partType , partDesc.partName , actionDes.actionName + dirStr);
						}

						actionDes.partsDescription[partDesc.partName] = partDesc;
					}
				}

				actionsDescription[actionDes.actionName] = actionDes;
			}
		}

		/**
		 * 获得该人物的一组动作描述
		 * @param actionName
		 * @return
		 *
		 */
		public function getActionDescriptionByName(actionName : String) : AvatarActionDescription
		{
			return actionsDescription[actionName];
		}


		/**
		 * 根据部件名称获得部件的类别
		 * @param partName
		 * @return
		 *
		 */
		public function getPartType(partName : String) : String
		{
			for each(var actionDes : AvatarActionDescription in actionsDescription)
			{
				for each(var partDesc : AvatarPartDescription in actionDes.partsDescription)
				{
					if(partDesc.partName == partName)
					{
						return partDesc.partType;
					}
				}
			}
			return null;
		}

	}
}